using System;
using System.Text;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using XjjXmm.Infrastructure.Ioc;
using XjjXmm.Infrastructure.ToolKit;
using xjjxmm.infrastructure.Webs;
using System.Text.RegularExpressions;

namespace XjjXmm.Infrastructure.Exceptions;

public partial class BussinessException : Exception
{

	public static BussinessException Create(string code)
	{
		var msg = GetBussinessMessage( code);
		if (msg == null)
		{
			throw Create(BussinessStatusCodes.NOT_EXISTS);
		}
       
		return new BussinessException(msg.Message, msg.Code, null);
	}  
    
	public static BussinessException Create(string code, Exception? innerException )
	{
		var msg = GetBussinessMessage(code);
		if (msg == null)
		{
			throw Create(BussinessStatusCodes.NOT_EXISTS);
		}
       
		return new BussinessException(msg.Message, msg.Code, innerException);
	}
   
	public static BussinessException Create(string code, params object?[] args)
	{
		return Create(code, args, null);
	}
   
	public static BussinessException Create(string code, object?[] args, Exception? innerException)
	{
		var msg = GetBussinessMessage(code);
		if (msg == null)
		{
			throw Create(BussinessStatusCodes.NOT_EXISTS);
		}
       
		return new BussinessException(string.Format(msg.Message, args), msg.Code, innerException);
	}
	
	private static BussinessMessage? GetBussinessMessage(string code)
	{
		//ThreadLocalContext context = IocManager.Instance.GetService<ThreadLocalContext>();
		//var cultures = context.Get<List<string>>(CacheKey.CULTURE)!;

		//var path = Path.Combine(AppKit.GetCurrentDirectory() ,CommonConstant.Culture);

       // foreach (var culture in cultures)
       // {
			//var culturePath = Path.Combine(path, $"{culture}.json");
        //}       //var messages =
                //ConfigHelper.GetSection<List<ExceptionMessage>>(CommonConstant.Culture, CommonConstant.BussinessMessage);

        /*var index = messages?.FindIndex(m => m.Code == code);
		
		var messageDictionary = ConfigHelper.GetSection<Dictionary<string, string>>(CommonConstant.Culture, CommonConstant.BussinessMessage, index?.ToString()??"0", "message");
		var message = "";
		foreach (var culture in cultures)
		{
			if (messageDictionary!.ContainsKey(culture))
			{
				message = messageDictionary[culture];
				break;
			}
		}*/

        return new BussinessMessage()
		{
			Code = code,
			Message = code
		};
	}
	
	/// <summary>
	/// 错误码
	/// </summary>
	public string? Code { get; set; }

	//public override string Message => GetMessage();
	public BussinessException(Exception innerException) : this(null, null, innerException)
	{

	}

    public BussinessException(string message) : this(message, null,  null)
	{

	}

    public BussinessException(string message, string code) : this(message, code,  null)
	{

	}

    public BussinessException(string message, Exception innerException) : this(message,null, innerException)
	{

	}

    public BussinessException(string? message, string? code = null,  Exception? innerException = null) : base(message, innerException)
	{
		Code = code;
	}

	/// <summary>
	/// 获取错误消息
	/// </summary>
	private string GetMessage()
	{
		return GetMessage(this);
	}

	/// <summary>
	/// 获取错误消息
	/// </summary>
	private string GetMessage(Exception ex)
	{
		var result = new StringBuilder();
		var list = GetExceptions(ex);
		foreach (var exception in list)
			AppendMessage(result, exception);
		return result.ToString();
	}

	/// <summary>
	/// 添加异常消息
	/// </summary>
	private void AppendMessage(StringBuilder result, Exception? exception)
	{
		if (exception == null)
			return;
		
		result.AppendLine(exception.Message);
	}

	/// <summary>
	/// 获取异常列表
	/// </summary>
	private IList<Exception> GetExceptions()
	{
		return GetExceptions(this);
	}

	/// <summary>
	/// 获取异常列表
	/// </summary>
	/// <param name="ex">异常</param>
	private IList<Exception> GetExceptions(Exception ex)
	{
		var result = new List<Exception>();
		AddException(result, ex);
		return result;
	}

	/// <summary>
	/// 添加内部异常
	/// </summary>
	private void AddException(List<Exception> result, Exception? exception)
	{
		if (exception == null)
			return;
		result.Add(exception);
		AddException(result, exception.InnerException);
	}
}
